//
//  MyAppCell.swift
//  TinyUI
//
//  Created by 徐涛 on 2020/3/26.
//  Copyright © 2020 葛晓良. All rights reserved.
//

import UIKit
import TinyUI
//行高86
//44 13
class MyAppCell: UICollectionViewCell {//缩起来时的的展示Cell
    var iconUIArr:Array = Array<Any>.init()
    let iconWidth:CGFloat = 44 //圆圈图标大小
    let iconImgWidth:CGFloat = 26 //圆圈图标大小
    let intrusion:CGFloat = 13 //圆叠起来的距离
    //     let borderWidth:CGFloat = 2//白边距离
    var maxCount = 6 //最多APP 超过6个点击更多
    //未实现
    var request: AnyObject?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    required init!(element: Element!) {
        fatalError("init(element:) has not been implemented")
    }
    
    
    func configWithParam(param:Array<MicroAppModels>,limit:Int,dependence:NSDictionary)  {
        if param.count != 0 {
            if limit>0 {
                maxCount = (maxCount > limit) ? limit : maxCount
            }
            var max=param.count>maxCount ? maxCount:param.count
            max=max+1
            let imgAllWidet:CGFloat = iconWidth*CGFloat(max)-CGFloat(max-1)*intrusion
            let orgzinx=(self.bounds.width - imgAllWidet)/2
            let centery = self.bounds.height/2.0
            let centets_distance = iconWidth-intrusion
            iconUIArr.removeAll()
            self.subviews.forEach({ $0.removeFromSuperview() })
            self.layer.sublayers?.forEach { $0.removeFromSuperlayer() }
            var backColor:String="#ffffff";
            if let dColor:String = dependence["item-collapse-background-color"] as? String{
                backColor = dColor
            }
            for i in 0..<max {""
                let iconCarryView=UIView.init(frame: CGRect(x: 0, y: 0, width: iconWidth, height: iconWidth))
                let  iconView = UIImageView.init(frame: CGRect(x: 0, y: 0, width: iconImgWidth, height: iconImgWidth))
                
                //                            iconCarryView.layer.borderColor = UIColor.white.cgColor
                //                            iconCarryView.layer.borderWidth = borderWidth
                iconCarryView.layer.masksToBounds = true
                iconCarryView.layer.cornerRadius = iconWidth/2.0
                iconCarryView.backgroundColor=UIColor.tyColor(withHex: backColor)
                
                
                iconView.layer.cornerRadius = iconImgWidth/2.0
                iconView.backgroundColor=UIColor.tyColor(withHex: backColor)
                iconView.contentMode = UIView.ContentMode.scaleAspectFill
                
                iconCarryView.addSubview(iconView)
                iconView.center=CGPoint(x: iconCarryView.bounds.width/2.0, y: iconCarryView.bounds.height/2.0)
                if i==0 {
                    iconCarryView.center=CGPoint(x: orgzinx+iconWidth/2.0, y: centery)
                    //                    iconView.backgroundColor=UIColor.clear
                    self.addSubview(iconCarryView)
                    self.addShadow(caView: iconCarryView)
                    iconCarryView.layer.zPosition = CGFloat(i+1)
                    iconUIArr.append(iconCarryView)
                }else{
                    iconCarryView.center=CGPoint(x: orgzinx+iconWidth/2.0+CGFloat(i)*centets_distance, y: centery)
                    //                    iconView.backgroundColor=UIColor.clear
                    self.addSubview(iconCarryView)
                    self.addShadow(caView: iconCarryView)
                    iconCarryView.layer.zPosition = CGFloat(i+1)
                    iconUIArr.append(iconCarryView)
                }
                if i == max-1 {
                    iconView.image = UIImage(named:"MicroAppStore.bundle/more")
                }else{
                    let model:MicroAppModels=param[i]
                    //                                iconView.image = UIImage(named:"MicroAppStore.bundle/defalut")
                    if model.icon != nil{
                        loadImageWith(urlStr: model.icon!, imageV: iconView)
                    }
                }
            }
        }
    }
    
    func addShadow(caView:UIView)  {
        let shadowLayer:CALayer=CALayer.init()
        shadowLayer.frame = caView.frame;
        shadowLayer.backgroundColor = caView.backgroundColor?.cgColor;
        shadowLayer.cornerRadius = iconWidth * 0.5;
        shadowLayer.shadowColor = UIColor.black.cgColor
        shadowLayer.shadowOffset = CGSize.init(width: 3, height: 3);
        shadowLayer.shadowOpacity = 0.1;
        self.layer .addSublayer(shadowLayer);
    }
    
    func loadImageWith(urlStr:String,imageV:UIImageView)  {
        if !urlStr.isEmpty {
            guard let networkHelper: TinyUINetworkProtocol = TinyService.sharedInstance()?.instance(with: NSProtocolFromString("TinyUINetworkProtocol")!) as? TinyUINetworkProtocol else {
                return;
            }
            self.request = networkHelper.imageRequest(withUri: urlStr, relativeUrl: nil, option: nil, completion: {  [weak self] (data: Data?, error: Error?) in
                guard let r_data = data else {
                    imageV.image = UIImage(named:"MicroAppStore.bundle/defalut")
                    return;
                }
                imageV.image = UIImage.init(data: r_data)
            })as AnyObject?
        }
    }
    
}



